//
//  GDBuyCar.m
//  美团试验 (自己)
//
//  Created by 热带雨林 on 17/2/13.
//  Copyright © 2017年 热带雨林. All rights reserved.
//

#import "GDBuyCar.h"
#import "GDDetailsFoodModel.h"

//static UIButton *_showB;

@interface GDBuyCar()
//小红点
@property (weak, nonatomic) IBOutlet UIButton *buyCount;
@property (weak, nonatomic) IBOutlet UILabel *inforLabel;
//计算按钮
@property (weak, nonatomic) IBOutlet UIButton *buyButton;
//购物车
@property (weak, nonatomic) IBOutlet UIButton *showButton;


@end


@implementation GDBuyCar

+ (instancetype)buyCarinit{
    
    UINib *nib = [UINib nibWithNibName:@"GDBuyCar" bundle:nil];
    return [[nib instantiateWithOwner:nil options:nil]lastObject];
    
}
- (void)setBuyList:(NSArray<GDDetailsFoodModel *> *)buyList{
    
    _buyList = buyList;

//    _showButton = _showB;
    
    _buyCount.hidden = (buyList.count == 0);
    //购物车按钮的选中状态
    _showButton.selected = !(buyList.count == 0);
    _showButton.enabled = !(buyList.count == 0);

    
    //逻辑处理（价钱与个数的逻辑处理）
    //定义一个总价格数 和赏品总个数
    __block CGFloat allprice = 0;
    __block NSInteger allCount = 0;
    
    [buyList enumerateObjectsUsingBlock:^(GDDetailsFoodModel * _Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
       
        allCount += model.buyCount;
        //总价钱相加
        allprice += model.min_price.floatValue * model.buyCount;
    
    }];
    
    //小红点显示的个数
    [_buyCount setTitle:@(allCount).description forState:UIControlStateNormal];
    
    //修改按钮的背景图片
//    if(allCount > 0){
//        [_showButton setImage:[UIImage imageNamed:@"button_shoppingCart_noEmpty"] forState:UIControlStateSelected];
//    }
    
    //中间购物车逻辑处理
    if (allprice){
        _inforLabel.textColor = [UIColor redColor];
        _inforLabel.text = [NSString stringWithFormat:@"共%.2f元",allprice];
    }else{
        _inforLabel.textColor = [UIColor lightGrayColor];
        _inforLabel.text = @"空空如也";
    }
    
    //以20为总金额的逻辑处理
    CGFloat minPrice = allprice - 20;
    if(minPrice >= 0){
        
        _buyButton.backgroundColor = [UIColor orangeColor];
        
        [_buyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
        [_buyButton setTitle:@"计算" forState:UIControlStateNormal];
        
    }else{
        _buyButton.backgroundColor = [UIColor grayColor];
        
        [_buyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        NSString *minStr = [NSString stringWithFormat:@"还差%.2f",ABS(minPrice)];
        [_buyButton setTitle:minStr forState:UIControlStateNormal];
    }
    
    _showButton.transform = CGAffineTransformMakeScale(0.8, 0.8);
    
    [UIView animateWithDuration:2 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
        
        _showButton.transform =CGAffineTransformIdentity;
        
    } completion:nil];
    
    
}

- (IBAction)clickButton:(UIButton *)sender {

    if([_delegate respondsToSelector:@selector(GDBuyCarController:withSelectedArray:)]){
        
        [_delegate GDBuyCarController:self withSelectedArray:_buyList];
        
    }
    
}







@end
